package com.bbm.communitysystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Car;
import com.bbm.communitysystem.domain.dto.CarDto;
import com.bbm.communitysystem.domain.vo.CarVo;
import com.bbm.communitysystem.service.ICarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "车辆")
@RestController
@RequestMapping("/car")
@RequiredArgsConstructor
public class CarController {

    private final ICarService carService;

    @Operation(summary = "创建车辆信息")
    @PostMapping
    public R<String> createCar(@RequestBody CarDto carDto) {

        Car car = BeanUtil.copyProperties(carDto, Car.class);

        car.setUpdateTime(System.currentTimeMillis() + "");

        boolean save = carService.save(car);


        return save ? R.ok("保存成功") : R.error("保存失败");
    }

    @Operation(summary = "查询车辆信息")
    @GetMapping("/{id}")
    public R<CarVo> getCarById(@PathVariable Long id) {

        Car car = carService.getById(id);

        CarVo carVo = BeanUtil.copyProperties(car, CarVo.class);
        return R.ok(carVo);
    }

    @Operation(summary = "更新车辆信息")
    @PutMapping
    public R<String> updateCar(@RequestBody CarDto carDto) {

        Car car = BeanUtil.copyProperties(carDto, Car.class);

        boolean update = carService.updateById(car);

        return update ? R.ok("更新成功") : R.error("更新失败");
    }


    @Operation(summary = "删除车辆信息")
    @DeleteMapping("/{id}")
    public R<String> deleteCar(@PathVariable("id") Integer id) {

        return carService.removeById(id) ? R.ok("删除成功") : R.error("删除失败");
    }



}